package com.gel.shortLink.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gel.shortLink.admin.common.biz.UserContext;
import com.gel.shortLink.admin.convention.exception.ServiceException;
import com.gel.shortLink.admin.dao.entity.GroupDO;
import com.gel.shortLink.admin.dao.mapper.GroupDOMapper;
import com.gel.shortLink.admin.dto.req.ShortLinkGroupSortReqDTO;
import com.gel.shortLink.admin.dto.req.ShortLinkGroupUpdateReqDTO;
import com.gel.shortLink.admin.dto.resp.ShortLinkGroupRespDTO;
import com.gel.shortLink.admin.service.GroupService;
import com.gel.shortLink.admin.util.RandomStringUtils;
import org.bouncycastle.crypto.agreement.jpake.JPAKEPrimeOrderGroup;
import org.springframework.stereotype.Service;

import java.rmi.server.ServerCloneException;
import java.sql.Wrapper;
import java.util.List;
import java.util.Optional;

/**
* @author MJB_Computer
* @description 针对表【t_group】的数据库操作Service实现
* @createDate 2025-07-08 16:28:18
*/
@Service
public class GroupServiceImpl extends ServiceImpl<GroupDOMapper, GroupDO>
    implements GroupService {

    @Override
    public void saveGroup(String name) {
        saveGroup(UserContext.getUsername(),name);
    }

    @Override
    public void saveGroup(String username ,String name) {
        String gid;
        do{
            gid = RandomStringUtils.generate6Digit();

        }while (hasGid(username,gid));
        GroupDO groupDO = GroupDO.builder()
                .gid(gid)
                .name(name)
                .username(username)
                .build();
        int insert = baseMapper.insert(groupDO);
        if(insert <0){
            throw new ServiceException("新增短链接分组失败");
        }

    }

    @Override
    public List<ShortLinkGroupRespDTO> listGroup() {
        LambdaQueryWrapper<GroupDO> queryWrapper = Wrappers.lambdaQuery(GroupDO.class)
                .eq(GroupDO::getUsername, UserContext.getUsername())
                .eq(GroupDO::getDelFlag, 0)
                .orderByDesc(GroupDO::getSortOrder, GroupDO::getUpdateTime);
        List<GroupDO> groupDOS = baseMapper.selectList(queryWrapper);
        List<ShortLinkGroupRespDTO> shortLinkGroupRespDTOS = BeanUtil.copyToList(groupDOS, ShortLinkGroupRespDTO.class);
        return shortLinkGroupRespDTOS;

    }

    @Override
    public void updateGroup(ShortLinkGroupUpdateReqDTO requestParam) {
        LambdaUpdateWrapper<GroupDO> updateWrapper = Wrappers.lambdaUpdate(GroupDO.class)
                .eq(GroupDO::getGid, requestParam.getGid())
                .eq(GroupDO::getUsername, UserContext.getUsername())
                .eq(GroupDO::getDelFlag, 0);
        GroupDO groupDO = GroupDO.builder()
                .gid(requestParam.getGid())
                .name(requestParam.getName())
                .build();
        int update = baseMapper.update(groupDO, updateWrapper);
        if(update <0){
            throw new ServiceException( "更新短链接分组失败");
        }
    }

    @Override
    public void deleteGroup(String gid) {
        LambdaQueryWrapper<GroupDO> queryWrapper = Wrappers.lambdaQuery(GroupDO.class)
                .eq(GroupDO::getGid, gid)
                .eq(GroupDO::getUsername, UserContext.getUsername())
                .eq(GroupDO::getDelFlag, 0);

        int delete = baseMapper.delete(queryWrapper);
        if (delete <= 0){
            throw new ServiceException("删除分组失败");
        }
    }

    @Override
    public void sortGroup(List<ShortLinkGroupSortReqDTO> requestParam) {
        requestParam.forEach(each->{
            GroupDO groupDO = GroupDO.builder()
                    .sortOrder(each.getSortOrder())
                    .build();
            LambdaUpdateWrapper<GroupDO> updateWrapper = Wrappers.lambdaUpdate(GroupDO.class)
                    .eq(GroupDO::getUsername, UserContext.getUsername())
                    .eq(GroupDO::getGid, each.getGid())
                    .eq(GroupDO::getDelFlag, 0);
                    baseMapper.update(groupDO, updateWrapper);
        });
    }


    private Boolean hasGid(String username,String gid){
        LambdaQueryWrapper<GroupDO> queryWrapper = Wrappers.lambdaQuery(GroupDO.class)
                .eq(GroupDO::getGid, gid)
                .eq(GroupDO::getUsername, Optional.ofNullable(username).orElse(UserContext.getUsername()));
        GroupDO groupDO = baseMapper.selectOne(queryWrapper);
        if(groupDO == null){
            return false;
        }
        return true;

    }
}




